<?php
namespace app\api\controller\summary;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\power\Rolepower;
use app\api\controller\Base;
use app\common\model\company\Contract;
use app\common\model\cost\Costjournal;
use app\common\model\orderhawa\HawaList;
use app\common\model\orderdelivery\DeliveryList;
use app\common\model\orderreceipt\ReceiptList;
use app\common\model\customer\Customervisit;
class Order extends Base
{
	/**
	 * 产生jwt加密串，参考session
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
        Rolepower::powerguard(204002);
	}

	
	/**
	 * 获取订单信息
	 * @return [type] [description]
	 */
	public function getinfo(){
		$now = input('startValue');
		$end = input('endValue');
		$user = UserModel::getinfo();
		if(empty($now)||empty($end)){
			return myerr('缺少参数，请重新尝试');
		}
		$timestr = $now .'~' .$end;
		//统计数据
		if(Rolepower::ispower(203008)){
        	$sb = Costjournal::getsummary($now,$end);
        	if(!empty($sb)){
        		$rdata['summary'][] = ['合计支出',round($sb['PayCost']),$sb['PayCostUnit'],$timestr.'支出','public/list?type=costpay&starttime='.$now.'&endtime='.$end];
        		$rdata['summary'][] = ['合计收入',round($sb['RevenueCost']),$sb['RevenueCostUnit'],$timestr.'收入','public/list?type=costrevenue&starttime='.$now.'&endtime='.$end];
        	}
        }
        $sb = HawaList::getsummary($now,$end);
        if(!empty($sb)){
        	$str = 'manaorderhawa';
        	if($user->Role==203){
        		$str = 'orderhawaacco';
        	}
        	if(empty($sb['wait'])){
        		$rdata['summary'][] = ['产废回收',$sb['Weight'],$sb['WeightUnit'],$timestr.'回收','public/list?type='.$str .'&starttime='.$now.'&endtime='.$end];
        	}else{
        		$rdata['summary'][] = ['产废回收',$sb['Weight'],$sb['WeightUnit'],$timestr.'回收','public/list?type='.$str .'&starttime='.$now.'&endtime='.$end,$sb['wait'].'笔待结算'];	
        	}
        }
        $rdata['hawa'] = $sb['goodlist'];

        $sb = ReceiptList::getsummary($now,$end);
        if(!empty($sb)){
        	$str = 'manaorderreceipt';
        	if($user->Role==203){
        		$str = 'orderreceiptacco';
        	}
        	if(empty($sb['wait'])){
        		$rdata['summary'][] = ['入库订单',$sb['Weight'],$sb['WeightUnit'],$timestr.'入库','public/list?type='.$str.'&starttime='.$now.'&endtime='.$end];
        	}else{
        		$rdata['summary'][] = ['入库订单',$sb['Weight'],$sb['WeightUnit'],$timestr.'入库','public/list?type='.$str.'&starttime='.$now.'&endtime='.$end,$sb['wait'].'笔待结算'];
        	}
        }
        $rdata['receipt'] = $sb['goodlist'];

        $sb = DeliveryList::getsummary($now,$end);
        if(!empty($sb)){
        	$str = 'manaorderdelivery';
        	if($user->Role==203){
        		$str = 'orderdeliveryacco';
        	}
        	if(empty($sb['wait'])){
        		$rdata['summary'][] = ['出库订单',$sb['Weight'],$sb['WeightUnit'],$timestr.'出库','public/list?type='.$str.'&starttime='.$now.'&endtime='.$end];
        	}else{
        		$rdata['summary'][] = ['出库订单',$sb['Weight'],$sb['WeightUnit'],$timestr.'出库','public/list?type='.$str.'&starttime='.$now.'&endtime='.$end,$sb['wait'].'笔待结算'];
        	}
        	
        }
        $rdata['delivery'] = $sb['goodlist'];
        //合同情况
        $sb = Contract::getsummary($now,$end);
        if(!empty($sb)){
        	$str = 'manacontract';
        	if($user->Role==203){
        		$str = 'contractacco';
        	}
        	$rdata['summary'][] = ['处置合同',$sb['Count'],$sb['CountUnit'],$timestr.'合同','public/list?type='.$str.'&starttime='.$now.'&endtime='.$end,$sb['Price'].$sb['PriceUnit']];
        }
        $userlist =[];
        $userid = [];
        $contract = $sb['userlist'];
        foreach ($contract as $key => $value) {
        	$userlist[$value['BussinessId']] = $value;
            $userlist[$value['BussinessId']]['VisitCount'] = 0;
            $userid[] = $value['BussinessId'];
        }
        // $rdata['contract'] = $sb['userlist'];
        //客户回访情况
        $customervisit = Customervisit::getsummary($now,$end);

        foreach ($customervisit as $key => $value) {
        	$userid[] = $key;
        	if(!empty($userlist[$key])){
        		$userlist[$key]['VisitCount'] = $value['Count'];
        	}else{
        		$userlist[$key] = ['BussinessId'=>$key,'Price'=>0,'Count'=>0,'VisitCount'=>$value['Count']];
        	}
        }
        //将业务员名放入里面
        $userinfo = UserModel::where('UserId','in',$userid)->column('Name','UserId');
        $contract = [];
        foreach ($userinfo as $key => $value) {
        	$userlist[$key]['Name'] = $value;
        	$contract[] = $userlist[$key];
        }
        //将合同数/咨询数/合同费用整理放入
        $rdata['contract'] = $contract;

        //入库情况
        $sb = ReceiptList::getsummaryinfo($now,$end);
        if(!empty($sb)){
            //每日情况
            $rdata['datalist'] = $sb['datalist'];
            //收集员数据        
            $rdata['userlist'] = ['namelist'=>$sb['namelist'],'data'=>$sb['userlist']];
        }
		return myok($rdata);
	}


	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
